package deepbitview

/**
 * a simple way to access your wallet
 */
class WalletHandler {

    /**
     * is there already a wallet configured
     * @return
     */
    static boolean isConfigured() {
        boolean valid = Wallet.list().size() == 1

        if (Wallet.list().size() > 1) {

            //for some reasons there were more than 1 wallets registered, which should never be the case
            Wallet.list().each {Wallet wallet -> wallet.delete(flush: true)}
        }

        return valid
    }

    /**
     * the username of the wallet
     * @return
     */
    static String getUserName() {
        return Wallet.list().get(0).getUsername()
    }

    /**
     * get password
     * @return
     */
    static String getPassword() {
        return Wallet.list().get(0).getUsername()
    }

    /**
     * get the wallet address
     * @return
     */
    static String getAddress() {
        return Wallet.list().get(0).getUsername()
    }

    /**
     * registers or replaces an account
     * @param userName
     * @param password
     */
    static void register(String userName, String password, String address) {
        if (isConfigured()) {
            Wallet.list().get(0).delete(flush: true)
        }

        Wallet mt = new Wallet()
        mt.username = userName
        mt.password = password
        mt.address = address
        mt.save(flush: true)

    }
}
